package Tests.UnitTests;

import harima.collision.bodies.Direction;
import harima.collision.bodies.PathNode;
import harima.collision.bodies.PlatformPath;

import java.util.ArrayList;
import java.util.Random;

import junit.framework.Assert;
import junit.framework.TestCase;


public class PlatformPathFixure extends TestCase{
	
	PlatformPath path;
	Random random;
	
	public void init()
	{
		path = new PlatformPath();
		random= new Random();
	}
	
	public void test_PathCanAddNodes_AddNodeRequested_Always() throws Exception {
		init();
		
		PathNode firstNode = new PathNode(Direction.Down, 0 );
		PathNode lastNode = new PathNode(Direction.Down, 0);
		path.addNode(firstNode);
		path.addNode(lastNode);
		
		Assert.assertNotNull(firstNode.nextNode);
		Assert.assertSame(firstNode.nextNode, lastNode);
		Assert.assertNull(lastNode.nextNode);
	}
	
	public void test_CanAddNodeAfterAnother_AddNodeRequested_Always() throws Exception {
		init();
		
		int nodeCount = random.nextInt(100)+1;
		
		PathNode previousNode = new PathNode(Direction.Down, 0);
		path.addNode(previousNode);
		
		
		for(int i=0; i<nodeCount; i++)
		{
			PathNode lastNode = new PathNode(Direction.Down, 0);
			path.addNode(lastNode);
			Assert.assertSame(lastNode, previousNode.nextNode);
			previousNode = lastNode;
		}
	}
	
	public void test_CanStepNodes_StepRequested_Always()
	{
		init();
		
		int nodeCount = random.nextInt(100)+1;
		int stepCount = random.nextInt(100)+1;

		ArrayList<PathNode> nodes = new ArrayList<PathNode>();
		
		for(int i=0; i<nodeCount; i++)
		{
			PathNode node = new PathNode(Direction.Down, stepCount);
			nodes.add(node);
			path.addNode(node);
		}
		
		for(int i=0; i<nodeCount-1; i++)
		{
			for(int j=0; j<stepCount-1; j++)
			{
				Assert.assertFalse(path.step());
			}
			Assert.assertTrue(path.step());
			Assert.assertNotNull(path.nextNode());
		}
		
	}
	
	public void test_ReturnsToFirstNode_NextNodeRequested_PathReachesLastNode() throws Exception {
		init();
		
		int nodeCount = random.nextInt(100) + 1;
		
		PathNode firstNode = new PathNode(Direction.Down, 0);
		path.addNode(firstNode);
		
		for(int i=0; i<nodeCount; i++)
		{
			path.nextNode();
		}
		
		PathNode firstNodeAgain = path.nextNode();
		
		Assert.assertNotNull(firstNodeAgain);
		Assert.assertSame(firstNode, firstNodeAgain);
	}
	
	public void test_ReturnsFirstNode_NextNodeRequested_FirstRequest() throws Exception {
		init();
		
		PathNode firstNode = new PathNode(Direction.Down, 0);
		path.addNode(firstNode);
		PathNode firstNodeAgain  = path.nextNode();
		Assert.assertNotNull(firstNode);
		Assert.assertSame(firstNode, firstNodeAgain);
	}
	
}
